package gvo.start;



import gvo.biblio.GVO_GUI;
import gvo.rero.ReroImport;
import gvo.sql.SQLDirekt;
import gvo.util.GVOstore;
import gvo.util.MyWindowClosingAdapter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import org.jdesktop.swingworker.SwingWorker;

public class ChoiceWindow extends JFrame implements ActionListener{
	private static final String TITEL = "GVO";
	private static final int BREITE = 250;
	private static final int HOEHE = 200;
	private static final String RERO = "rero";
	private static final String SQL = "sql";
	private static final String DB2 = "db2";
	private static final String DBMA = "dbma";
	
	public GVOstore gvostore = new GVOstore();
	
	public ChoiceWindow (){
		setTitle(TITEL);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setSize(new Dimension(BREITE, HOEHE));
		JPanel wahl = new JPanel();
		getContentPane().add(BorderLayout.CENTER, wahl);
		wahl.setLayout(new GridLayout(4, 1));
		JButton reroButton = new JButton("Rero Suche");
		reroButton.addActionListener(this);
		reroButton.setActionCommand(RERO);
		wahl.add(reroButton);
		JButton dbButton = new JButton("SQL Direkt");
		dbButton.setActionCommand(SQL);
		dbButton.addActionListener(this);
		wahl.add(dbButton);
		JButton db2Button = new JButton("Datenbank neu");
		db2Button.setActionCommand(DB2);
		db2Button.addActionListener(this);
		wahl.add(db2Button);
		JButton dbmaButton = new JButton("Datenbankmanager");
		dbmaButton.setActionCommand(DBMA);
		dbmaButton.addActionListener(this);
		wahl.add(dbmaButton);
		this.addWindowListener(new MyWindowClosingAdapter(gvostore.sqlTool));
		setVisible(true);
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 SwingUtilities.invokeLater(new Runnable() {
		      public void run() {
		        new ChoiceWindow();
		      }
		    });
		
		//new TestHtml();
	}
	
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals(RERO)) {
			new ReroImport(gvostore);
		} else if  (cmd.equals(SQL)) {
			new SQLDirekt(gvostore);
		} else if  (cmd.equals(DB2)) {
			if (gvostore.getGvoGUI()==null){
				gvostore.setGvoGUI(new GVO_GUI(gvostore));
			} else {
				gvostore.getGvoGUI().setVisible(true);
				gvostore.getGvoGUI().toFront();
			}
			
		} else if (cmd.equals(DBMA)){
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
				protected Void doInBackground() {
					String[] commands = new String[] { "java", "-cp",
							"./lib/hsqldb.jar",
							"org.hsqldb.util.DatabaseManager", "--url",
							"jdbc:hsqldb:file:/Users/rene/Documents/workspace/GVO Bibliothek/DB/biblio", "--user", "sa" };
					try {
						Runtime.getRuntime().exec(commands);
					} catch (IOException e) {
						e.printStackTrace();
					}
					return null;
				}
				public void done() {

				}
			};
			worker.execute();
		}
		
	}

}